// BlogCollectionController.java
package net.web.controller;

import net.web.common.utils.JSONUtil;
import net.web.common.utils.Result;
import net.web.service.BlogCollectionService;
import net.web.common.framework.simplemvc.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("/api/collection")
public class BlogCollectionController {
    private BlogCollectionService collectionService = new BlogCollectionService();

    @RequestMapping("/collect.do")
    public void collect(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        int userId = req.getParameter("user_id") != null ? Integer.parseInt(req.getParameter("user_id")) : Integer.parseInt(req.getParameter("userId"));
        boolean ok = collectionService.collectBlog(blogId, userId);
        JSONUtil.printByJSON(resp, ok ? Result.success("收藏成功") : Result.fail("收藏失败"));
    }
    @RequestMapping("/uncollect.do")
    public void uncollect(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        int userId = req.getParameter("user_id") != null ? Integer.parseInt(req.getParameter("user_id")) : Integer.parseInt(req.getParameter("userId"));
        boolean ok = collectionService.uncollectBlog(blogId, userId);
        JSONUtil.printByJSON(resp, ok ? Result.success("取消收藏成功") : Result.fail("取消收藏失败"));
    }
    @RequestMapping("/isCollected.do")
    public void isCollected(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        int userId = req.getParameter("user_id") != null ? Integer.parseInt(req.getParameter("user_id")) : Integer.parseInt(req.getParameter("userId"));
        JSONUtil.printByJSON(resp, Result.success(collectionService.isCollected(blogId, userId)));
    }
    @RequestMapping("/collectionCount.do")
    public void collectionCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        JSONUtil.printByJSON(resp, Result.success(collectionService.getCollectionCount(blogId)));
    }
    @RequestMapping("/myCollections.do")
    public void myCollections(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int userId = req.getParameter("user_id") != null ? Integer.parseInt(req.getParameter("user_id")) : Integer.parseInt(req.getParameter("userId"));
        JSONUtil.printByJSON(resp, Result.success(collectionService.getCollectionsByUserId(userId)));
    }
}
